package com.baosight.sczx.zm.ch.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.xerces.impl.dv.xs.DecimalDV;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.ch.domain.ZMCHProduceReport;
/**
 * 
 * <p>Title: ServiceZMCH70.java</p>
 * <p>Description: 生产进度旬验收</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 郭淑芳
 * @version 1.0
 * <pre>Histroy:
 *          2016年9月6日 上午10:03:27  郭淑芳  Create
 *</pre>
 */
public class ServiceZMCH70 extends ServiceEPBase {
	
	
	/**
	 * 
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p>
	 * <p>return value: ServiceZMCH70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月6日上午10:04:27 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);

		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false, UserSession.getInSessionProperty("miningCode").toString()));
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: query</p>
	 * <p>Description: 查询</p>
	 * <p>return value: ServiceZMCH70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月6日上午10:04:35 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdMiningId = UserSession.getInSessionProperty("miningCode").toString();
		StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);
		EiInfo outInfo = super.query(inInfo, "ZMCHProduceReport.query", new ZMCHProduceReport());

		EiBlock eiBlock = outInfo.getBlock("result");
		ZMCHProduceReport _dom = new ZMCHProduceReport();
		eiBlock.setBlockMeta(_dom.eiMetadata);
		for (int i = 0; i < eiBlock.getRowCount(); i++) {

			_dom.fromMap(eiBlock.getRow(i));
			BigDecimal fdHbb = _dom.getFdHbb();
			BigDecimal a = BigDecimal.valueOf(0);
			if (fdHbb.compareTo(a) == 0) {
				eiBlock.setCell(i, "fdHbb", "厚煤层面积为0");
			} else {
				eiBlock.setCell(i, "fdHbb", "1:" + fdHbb);
			}

		}
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cp.status", new String[]{"0", "1"}, new String[]{"草拟", "生效"}, true));//状态
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false, UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.getBlock("result").set("other", "详细");
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.ch.dateType", true));// 旬分类
		//月份
		outInfo.addBlock(StaticBlocks.getPlanMonth("cmes.cp.planMonth", true));
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: insert</p>
	 * <p>Description: 新增方法</p>
	 * <p>return value: ServiceZMCH70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月6日上午10:06:27 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCHProduceReport _dom = new ZMCHProduceReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
		
			Map<String, String> params = new HashMap<String, String>();
			boolean validate = validate(inInfo, _dom, params);
			if (validate) {
				inInfo = this.query(inInfo);
				return inInfo;
			} else {
				_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());
				_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());
				_dom.setFdCurrentDate(StaticTools.getDate());
				_dom.setFdReportDepartment(UserSession.getInSessionProperty("orgName").toString());
				_dom.setFdReportDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
				_dom.setFdReporter(UserSession.getInSessionProperty("UserCname").toString());
				_dom.setFdReporterId(UserSession.getUserId().toString());

				_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CH_PRODUCEREPORT"));
				this.dao.insert("ZMCHProduceReport.insert", _dom);
				
				//填写旬初始日期
				String fdDateType = _dom.getFdDateType();
				String fdMonth = _dom.getFdMonth();
				String fdYear = _dom.getFdYear().toString();
				String fdVirtualDate = _dom.getFdVirtualDate();
				if(fdDateType.equals("4c9abb8bc0a801c30198f5833986f3c3")){
					fdVirtualDate = fdYear + "-" + fdMonth + "-01";
				}else if(fdDateType.equals("4c9abb93c0a801c30198f583f36ce365")){
					fdVirtualDate = fdYear + "-" + fdMonth + "-11";
				}else if(fdDateType.equals("4c9abb9bc0a801c30198f58334b1a863")){
					fdVirtualDate = fdYear + "-" + fdMonth + "-21";
				}
				_dom.setFdVirtualDate(fdVirtualDate);
				this.dao.update("ZMCHProduceReport.update", _dom);

				this.dao.update("ZMCHProduceReport.updateForMain", _dom); // 更新累计量
			}
		}

		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;

	}

	/**
	 * 
	 * <p>Function: update</p>
	 * <p>Description: 修改方法</p>
	 * <p>return value: ServiceZMCH70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月6日上午10:06:38 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCHProduceReport _dom = new ZMCHProduceReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));

			boolean canDelete = this.canDelete(inInfo, _dom.getFdKeyId());
			if (!canDelete) {
				out = this.query(inInfo);
				out.setStatus(-1);
				String msg = "该记录下有采煤/掘进数据存在,不能删除修改！";
				out.setMsg(msg);
				return out;
			}

			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());

			boolean validate = this.validate(inInfo, _dom, params);
			if (validate) {
				out = this.query(inInfo);
				return out;
			} else {
				
				//填写旬初始日期
				String fdDateType = _dom.getFdDateType();
				String fdMonth = _dom.getFdMonth();
				String fdYear = _dom.getFdYear().toString();
				String fdVirtualDate = _dom.getFdVirtualDate();
				if(fdDateType.equals("4c9abb8bc0a801c30198f5833986f3c3")){
					fdVirtualDate = fdYear + "-" + fdMonth + "-01";
				}else if(fdDateType.equals("4c9abb93c0a801c30198f583f36ce365")){
					fdVirtualDate = fdYear + "-" + fdMonth + "-11";
				}else if(fdDateType.equals("4c9abb9bc0a801c30198f58334b1a863")){
					fdVirtualDate = fdYear + "-" + fdMonth + "-21";
				}
				_dom.setFdVirtualDate(fdVirtualDate);
				
				this.dao.update("ZMCHProduceReport.update", _dom);
				this.dao.update("ZMCHProduceReport.updateForMain", _dom); // 更新累计量
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;

	}
	
	/**
	 * 
	 * <p>Function: delete</p>
	 * <p>Description: 删除方法</p>
	 * <p>return value: ServiceZMCH70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月6日上午10:06:47 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {

		EiInfo outInfo = null;

		for (int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++) {
			String fdCheckKey = inInfo.getCellStr("result", i, "fdKeyId");
			boolean canDelete = this.canDelete(inInfo, fdCheckKey);
			if (!canDelete) {
				outInfo = this.query(inInfo);
				outInfo.setStatus(-1);
				String msg = "该记录下有采煤/掘进数据存在,不能删除该记录！";
				outInfo.setMsg(msg);
				return outInfo;
			}
			EiInfo _temp = super.delete(inInfo, "ZMCHProduceReport.delete");
			outInfo = this.query(inInfo);
			outInfo.setMsg(_temp.getMsg());
			outInfo.setDetailMsg(_temp.getDetailMsg());
		}

		return outInfo;
	}

	/**
	 * 
	 * <p>Function: canDelete</p>
	 * <p>Description: 判断主表记录可否删除、修改(当下面有采煤、掘进数据时，不可删除/修改）</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年8月1日下午4:16:40 郭淑芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean canDelete(EiInfo inInfo, String fdCheckKey) {

		HashMap<String, String> params = new HashMap<String, String>();
		params.put("fdCheckKey", fdCheckKey);
		
		List _cmResult = dao.query("ZMCHMineValidateData.query", params); // 采煤
		List _jjResult = dao.query("ZMCHGrubValidateData.query", params); // 掘进
		
		if (_cmResult == null || _cmResult.isEmpty()) {
			return true;
		} else if (_jjResult == null || _jjResult.isEmpty()) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * 
	 * <p>Function: validate</p>
	 * <p>Description: 新增修改重复性校验</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年8月2日下午1:46:28 郭淑芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, ZMCHProduceReport _dom, Map<String, String> params) {
		if (params == null) {
			params = new HashMap<String, String>();
		}
		String fdMiningId = _dom.getFdMiningId();
		String fdYear = _dom.getFdYear().toString();
		String fdMonth = _dom.getFdMonth().toString();
		String fdDateType = _dom.getFdDateType();

		params.put("fdMiningId", fdMiningId);
		params.put("fdYear", fdYear);
		params.put("fdMonth", fdMonth);
		params.put("fdDateType", fdDateType);

		List result = dao.query("ZMCHProduceReport.validate", params);
		if (result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "生产进度旬汇报每矿每旬只能填写一份！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	}

	/**
	 * 
	 * <p>Function: report</p>
	 * <p>Description: 上报</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月2日下午3:21:37 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo) {

		// 判断是否有明细
		boolean validate = this.canDelete(inInfo, inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		if (validate) {
			inInfo.setStatus(-1);
			inInfo = this.query(inInfo);
			inInfo.setMsg("该生产进度旬汇报中没有采煤或掘进数据，请添加后上报！");
			return inInfo;
		}

		EiInfo _temp = super.update(inInfo, "ZMCHProduceReport.report");
		EiInfo outInfo = this.query(inInfo);
		outInfo.setMsg(_temp.getMsg());
		outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
} 